From ec339c8e4900fde48719283226c3286d23be15f8 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 29 Sep 2024 12:09:31 -0500 Subject: [PATCH] Normalize newlines of sql from textarea and for sql output --- src/pgwui_sql/lib.py | 26 ++++++++++++++++++++ src/pgwui_sql/templates/sql.mak | 4 +++ src/pgwui_sql/views/base.py | 3 ++- tests/test_lib.py | 43 +++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/pgwui_sql/lib.py create mode 100644 tests/test_lib.py diff --git a/src/pgwui_sql/lib.py b/src/pgwui_sql/lib.py new file mode 100644 index 0000000..3d2972d --- /dev/null +++ b/src/pgwui_sql/lib.py @@ -0,0 +1,26 @@ +# Copyright (C) 2024 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_SQL. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +import io + + +def normalize_newlines(sval): + '''Apply the Python io module's univeral newline transalation to a string + ''' + return io.StringIO(sval, newline=None).getvalue() diff --git a/src/pgwui_sql/templates/sql.mak b/src/pgwui_sql/templates/sql.mak index c916419..c3726d5 100644 --- a/src/pgwui_sql/templates/sql.mak +++ b/src/pgwui_sql/templates/sql.mak @@ -34,6 +34,7 @@ <%! + import pgwui_sql.lib from pgwui_common.path import asset_abspath sql_base_mak = asset_abspath('pgwui_sql:templates/sql_base.mak') @@ -83,6 +84,9 @@ }!

% if sql: + <% if upload_sql: + sql = pgwui_sql.lib.normalize_newlines(sql) + %>
    % for sql_line in sql.rstrip().split('\n'):
  1. ${sql_line}
  2. diff --git a/src/pgwui_sql/views/base.py b/src/pgwui_sql/views/base.py index 97197bc..8aa163b 100644 --- a/src/pgwui_sql/views/base.py +++ b/src/pgwui_sql/views/base.py @@ -20,6 +20,7 @@ import attrs import pgwui_core.core import pgwui_core.forms +import pgwui_sql.lib import wtforms.fields from pgwui_core.constants import ( @@ -123,7 +124,7 @@ class SQLBaseForm(pgwui_core.forms.UploadFormBaseMixin, elif self._form.sql.data is None: self['sql'] = '' else: - self['sql'] = self._form.sql.data + self['sql'] = pgwui_sql.lib.normalize_newlines(self._form.sql.data) self['null_rep'] = self._form.null_rep.data self['download'] = self._form.download.data diff --git a/tests/test_lib.py b/tests/test_lib.py new file mode 100644 index 0000000..955cdfb --- /dev/null +++ b/tests/test_lib.py @@ -0,0 +1,43 @@ +# Copyright (C) 2024 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_SQL. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +import pgwui_sql.lib as lib +import pytest + + +# Activiate the PGWUI pytest plugin +pytest_plugins = ("pgwui",) + + +# normalize_newlines() +@pytest.mark.unittest +@pytest.mark.parametrize( + 'val,expected', + [('this has no newlines', 'this has no newlines'), + ('this\nhas\n newlines\n', 'this\nhas\n newlines\n'), + ('this\r\nhas\r\n newlines\r\n', 'this\nhas\n newlines\n'), + ('this\rhas\r newlines\r', 'this\nhas\n newlines\n')], + ids=('no_newlines', 'newlines', 'crlf', 'cr')) +def test_normalize_newlines(val, expected): + '''The expected normalization happens + ''' + result = lib.normalize_newlines(val) + assert result == expected -- 2.34.1